package com.simpleaccount.Graph;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.db.chart.animation.Animation;
import com.db.chart.model.Bar;
import com.db.chart.model.BarSet;
import com.db.chart.renderer.AxisRenderer;
import com.db.chart.view.HorizontalStackBarChartView;
import com.simpleaccount.Common.DateUtils;
import com.simpleaccount.Db.DbUtils;
import com.simpleaccount.Entity.Category;
import com.simpleaccount.Entity.Entry;
import com.simpleaccount.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import lecho.lib.hellocharts.model.ColumnChartData;

/**
 * Created by Administrator on 2017/7/12.
 */

//public class ColumnChartFragment extends Fragment {
//    @Bind(R.id.ccv_1)
//    ColumnChartView ccv1;
//    @Bind(R.id.pccv_1)
//    PreviewColumnChartView pccv1;
//    private List<Category> categoryList;
//    private HashMap<String, Float> rawData;
//    private ColumnChartData data;
//    private ColumnChartData previewData;
//    private Bundle bundle;
//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        bundle =getArguments();
//    }
//
//    @Nullable
//    @Override
//    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        categoryList = getCategoryList();
//        View view = inflater.inflate(R.layout.fragment_columnchart, container, false);
//        ButterKnife.bind(this, view);
//        getRawData(bundle.getLong("date"));
//        initColumnChartView();
//        pccv1.setViewportChangeListener(new ViewportListener());
//        return view;
//    }
//    public static ColumnChartFragment newInstance(long date) {
//        ColumnChartFragment myFragment = new ColumnChartFragment();
//        Bundle args = new Bundle();
//        args.putLong("date", date);
//        myFragment.setArguments(args);
//        return myFragment;
//    }
//    public void initColumnChartView() {
//        ccv1.setZoomEnabled(false);
//        initChartData();
//    }
//
//    private void initChartData() {
//        if(0==rawData.size()){
//            return;
//        }
//        int cntSubCol = 1;
//        int cntCol = categoryList.size();
//        List<Column> columns = new ArrayList<>();
//        List<SubcolumnValue> values;
//        List<AxisValue> axisValues = new ArrayList<>();
//        for (int i = 0; i < cntCol; i++) {
//            values = new ArrayList<>();
//            //子列操作
//            for (int j = 0; j < cntSubCol; j++) {
//                Float f = rawData.get(categoryList.get(i).getName());
//                values.welcome_add(new SubcolumnValue(null==f?0:f, categoryList.get(i).getColor()));
//            }
//            //
//
//            Column col = new Column(values);
//            ColumnChartValueFormatter chartValueFormatter = new SimpleColumnChartValueFormatter(2);
//            col.setFormatter(chartValueFormatter);
//            col.setHasLabels(true);
//            col.setHasLabelsOnlyForSelected(false);
//            columns.welcome_add(col);
//            axisValues.welcome_add(new AxisValue(i).setLabel(categoryList.get(i).getName()));
//
//        }
//        data = new ColumnChartData(columns);
//        Axis axisX = new Axis();
//        Axis axisY = new Axis().setHasLines(true);
//        axisY.setName("支出金额");
//        axisY.hasLines();//是否显示网格线
//        axisY.setTextColor(R.color.Black);//颜色
//        axisX.hasLines();
//        axisX.setTextColor(R.color.Black);
//        axisX.setValues(axisValues);
//        data.setAxisXBottom(axisX);
//        data.setAxisYLeft(axisY);
//        ccv1.setColumnChartData(data);
//
//        previewData = new ColumnChartData(data);
//        for (Column column : previewData.getColumns()) {
//            for (SubcolumnValue value : column.getValues()) {
//                value.setColor(ChartUtils.DEFAULT_DARKEN_COLOR);
//            }
//        }
//    }
//
//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//        ButterKnife.unbind(this);
//    }
//
//    private List<Category> getCategoryList() {
//        return DbUtils.getCategoryList();
//    }
//
//    private void getRawData(long date) {
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(date);
//        List<Entry> entries = DbUtils.queryByDate(DateUtils.getTimesMonthmorning(calendar),DateUtils.getTimesMonthnight(calendar));
//        rawData = new HashMap<>();
//        for (Entry e : entries) {
//            String cate = e.getCategoryList();
//            Float sum = null==rawData.get(cate)?0:rawData.get(cate);
//            rawData.put(cate, (float) e.getMoney() + sum );
//        }
//    }
//    private class ViewportListener implements ViewportChangeListener {
//        @Override
//        public void onViewportChanged(Viewport newViewport) {
//            ccv1.setCurrentViewport(newViewport);
//        }
//
//    }
//}
public class ColumnChartFragment extends Fragment {
    @Bind(R.id.hsbcv_1)
    HorizontalStackBarChartView hsbcv1;

    private List<Category> categoryList;
    private HashMap<String, Float> rawData;
    private ColumnChartData data;
    private ColumnChartData previewData;
    private Bundle bundle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bundle =getArguments();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_columnchart, container, false);
        ButterKnife.bind(this, view);
        categoryList = getCategoryList();
        getRawData(bundle.getLong("date"));
        for(Category cate:categoryList){
            if(rawData.get(cate.getName())!=0f){
                initChart();
                inflater.inflate(R.layout.fragment_graph,null,false).findViewById(R.id.tv_hint).setVisibility(View.INVISIBLE);
                return view;
            }
        }
        view.setVisibility(View.INVISIBLE);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private List<Category> getCategoryList() {
        return DbUtils.getCategory(bundle.getInt("stat"));
    }

    public static ColumnChartFragment newInstance(long date,int stat) {
        ColumnChartFragment myFragment = new ColumnChartFragment();
        Bundle args = new Bundle();
        args.putLong("date", date);
        args.putInt("stat",stat);
        myFragment.setArguments(args);
        return myFragment;
    }
    public void initChart(){
        List<Bar> barSet=new ArrayList<>();
        BarSet sets=new BarSet();
        for(int i = 0; i< categoryList.size(); i++){
            Float f = rawData.get(categoryList.get(i).getName());
            if(f!=0f){
                Bar bar=new Bar(categoryList.get(i).getName(),Math.abs(f));
                bar.setColor(categoryList.get(i).getColor());
                barSet.add(bar);
            }


        }
        Collections.sort(barSet,new BarCmptr());
        for(Bar bar:barSet){
            sets.addBar(bar);
        }

        hsbcv1.setYLabels(AxisRenderer.LabelPosition.OUTSIDE);
        hsbcv1.addData(sets);
        hsbcv1.show(new Animation(500));
    }
    private void getRawData(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        List<Entry> entries = DbUtils.queryByDate(DateUtils.getTimesMonthmorning(calendar), DateUtils.getTimesMonthnight(calendar));
        rawData = new HashMap<>();
        for(Category cate: categoryList){
            rawData.put(cate.getName(),0f);
        }
        for (Entry e : entries) {
            String cate = e.getCategory();
            Float sum =  rawData.get(cate);
            if(sum!=null){
                rawData.put(cate, (float) e.getMoney() + sum);
            }
        }
    }
    public static final class BarCmptr implements Comparator<Bar>{

        @Override
        public int compare(Bar object1, Bar object2) {
            float m1=object1.getValue();
            float m2=object2.getValue();
            int result=0;
            if(m1>m2)
            {
                result=1;
            }
            if(m1<m2)
            {
                result=-1;
            }
            return result;
        }

    }
}